package com.appbee.news.client.ui;

import com.appbee.core.client.cell.EditableCell;
import com.appbee.core.client.cell.ICellEditListener;
import com.appbee.core.client.design.editablecelllist.EditableCellListResources;
import com.appbee.core.client.templates.CommonTemplates;
import com.appbee.core.shared.model.EmailAddressWrapper;
import com.appbee.news.shared.NewsProxy;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;

public class NewsCell extends EditableCell<NewsProxy>
{
	private final CommonTemplates html;
	private final DateTimeFormat timestampFormat = DateTimeFormat
			.getFormat(PredefinedFormat.DATE_TIME_SHORT);
	private final INewsStyle newsStyle;

	public NewsCell(boolean editMode,
					CommonTemplates html,
					INewsStyle newsStyle,
					EditableCellListResources listRes,
					ICellEditListener<NewsProxy> listener)
	{
		super(editMode, html, listRes.cellListStyle(), listener);
		this.newsStyle = newsStyle;
		this.html = html;
	}

	@Override
	public void render(Context context, NewsProxy value, SafeHtmlBuilder sb)
	{
		sb.append(html.div(newsStyle.newsTitle(), value.getTitle() == null ? ""
				: value.getTitle()));
		sb.append(html.div(newsStyle.newsMain(), SafeHtmlUtils
				.fromTrustedString(value.getContent() == null ? "" : value
						.getContent())));
		StringBuffer info = new StringBuffer();
		if (value.getEditUserEmail() != null)
		{
			info.append(new EmailAddressWrapper(value.getEditUserEmail())
					.getUserName());

		}
		if (value.getCreateDateTime() != null)
		{
			info.append(" ");
			info.append(timestampFormat.format(value.getCreateDateTime()));
		}
		sb.append(html.div(newsStyle.newsInfo(), info.toString()));
		super.render(context, value, sb);
	}

}
